#define _CRT_SECURE_NO_WARNINGS 1
#include<vector>
#include<queue>
using namespace std;
 struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 };
 
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root)
    {
        vector<vector<int>> vv;
        if (root == nullptr)
        {
            return vv;
        }
        queue<TreeNode*> q;
        q.push(root);
        int count = 1;
        while (!q.empty())
        {
            int newcount = 0;
            vector<int> v;
            while (count--)
            {
                TreeNode* tmp = q.front();
                q.pop();
                if (tmp->left != nullptr)
                {
                    q.push(tmp->left);
                    newcount++;
                }
                if (tmp->right != nullptr)
                {
                    q.push(tmp->right);
                    newcount++;
                }
                v.push_back(tmp->val);
            }
            count = newcount;
            vv.push_back(v);
        }
        reverse(vv.begin(), vv.end());
        return vv;
    }
};
int main()
{
    Solution s;
    s.levelOrderBottom(nullptr);
    return 0;
}